package com.example.controller.admin;

import com.example.pojo.*;
import com.example.pojo.admin.SearchBean;
import com.example.service.ArticleService;
import com.example.service.CategoryService;
import com.example.service.ChatLogService;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @Description TODO
 * @Author fantasy
 * @Date 2024/7/16 15:29
 **/

@Controller
@RequestMapping("/admin")
public class ManageController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private UserService userService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/user")
    public String getUserList(Model model){
        List<User> users = userService.getUserList();
        model.addAttribute("users",users);
        return "user/list";
    }
    @RequestMapping("/user/like")
    public String getUserLike(SearchBean searchBean, Model model){
        if (searchBean.getActive().equals("")){
            searchBean.setActive(null);
        }
        if (searchBean.getId().equals("")){
            searchBean.setId(null);
        }
        List<User> users = userService.getUserLikeList(searchBean);
        model.addAttribute("users",users);
        return "user/list";
    }
    @GetMapping("/user/{id}")
    public String getUser(@PathVariable("id") String id,Model model){
        if ("add".equals(id)) {
            return "user/add";
        }
        User user = userService.getUserById(Integer.valueOf(id));
        model.addAttribute("user",user);
        return "user/view";
    }
    @PostMapping("/user")
    public String addUser(@Validated(User.Add.class) User user, Model model){
        userService.addUser(user);
        List<User> users = userService.getUserList();
        model.addAttribute("users",users);
        return "redirect:/admin/user";
    }
    @PostMapping("/user/delete/{id}")
    public String deleteUser(@PathVariable Integer id,Model model){
        userService.delete(id);
        List<User> users = userService.getUserList();
        model.addAttribute("users",users);
        return "redirect:/admin/user";
    }
    @PostMapping("/user/revoke/{id}")
    public String revokeUser(@PathVariable Integer id,Model model){
        userService.revoke(id);
        List<User> users = userService.getUserList();
        model.addAttribute("users",users);
        return "redirect:/admin/user";
    }
    @GetMapping("/article")
    public String getArticleList(Model model){
        List<AdminArticle> articles = articleService.getAllList();
        model.addAttribute("articles",articles);
        List<User> users = userService.getUserList();
        model.addAttribute("users",users);
        return "article/list";
    }
    @RequestMapping("/article/like")
    public String getArticleLike(SearchBean searchBean, Model model){
        if (searchBean.getActive().equals("")){
            searchBean.setActive(null);
        }
        if (searchBean.getId().equals("")){
            searchBean.setId(null);
        }
        if (searchBean.getState().equals("")){
            searchBean.setState(null);
        }
        List<AdminArticle> articles = articleService.getLikeList(searchBean);
        model.addAttribute("articles",articles);
        List<User> users = userService.getUserList();
        model.addAttribute("users",users);
        return "article/list";
    }
    @GetMapping("/article/{id}")
    public String getArticleById(@PathVariable Integer id,Model model) {
        Article article = articleService.getArticleById(id);
        model.addAttribute("article",article);
        return "article/view";
    }
    @PostMapping("/article/delete/{id}")
    public String deleteArticle(@PathVariable Integer id,Model model){
        articleService.delete(id);
        List<AdminArticle> articles = articleService.getAllList();
        model.addAttribute("articles",articles);
        return "redirect:/admin/article";
    }
    @RequestMapping("/article/revoke/{id}")
    public String revokeArticle(@PathVariable Integer id,Model model){
        articleService.revoke(id);
        List<AdminArticle> articles = articleService.getAllList();
        model.addAttribute("articles",articles);
        return "redirect:/admin/article";
    }
    @GetMapping("/category")
    public String getCategoryList(Model model){
        List<AdminCategory> categories = categoryService.getCategoryList();
        model.addAttribute("categories",categories);
        return "category/list";
    }
    @RequestMapping("/category/delete/{id}")
    public String deleteCategory(@PathVariable Integer id,Model model){
        categoryService.delete(id);
        List<AdminCategory> categories = categoryService.getCategoryList();
        model.addAttribute("categories",categories);
        return "redirect:/admin/category";
    }
    @RequestMapping("/category/revoke/{id}")
    public String revokeCategory(@PathVariable Integer id,Model model){
        categoryService.revoke(id);
        List<AdminCategory> categories = categoryService.getCategoryList();
        model.addAttribute("categories",categories);
        return "redirect:/admin/category";
    }
    @Autowired
    private ChatLogService chatLogService;
    @GetMapping("/chat")
    public String getChatList(Model model){
        List<ChatLog> chats = chatLogService.getChatList();
        model.addAttribute("chats",chats);
        return "chat/list";
    }

}
